#pragma once
#include <iostream>
#include <string>
#include <assert.h>

namespace zh
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}

		vector(const vector<T>& t)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			reserve(t.capacity());
			for (const auto& e : t)
				push_back(e);
		}

		void swap(vector<T>& v)
		{
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		iterator begin()
		{
			return _start;
		}

		const_iterator begin() const
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		void reserve(const size_t& n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				iterator tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * sz);
					for (size_t i = 0;i < sz;++i)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		T operator[](const size_t& i)
		{
			return *(_start + i);
		}

		const T operator[](const size_t& i) const
		{
			return *(_start + i);
		}

		void push_back(const T& i)
		{
			if (size() == capacity())
			{
				size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = i;
			++_finish;
		}

		void pop_back()
		{
			assert(_start < _finish);
			--_finish;
		}

		iterator find(const T& t)
		{
			iterator it = begin();
			while (it != end())
			{
				if (*it == t)
					return it;
				++it;
			}
		}

		void insert(const iterator& pos, const T& i)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			if (_finish == _endofstorage)
			{
				size_t n = pos - _start;
				size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + n;
			}
			iterator end = _finish-1;
			while (pos<=end)
			{
				*(end + 1) = *(end);
				--end;
			}
			*pos = i;
			++_finish;
		}

		void erase(const iterator& pos)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			iterator _pos =pos;
			while (_pos < _finish - 1)
			{
				*_pos = *(_pos + 1);
				++_pos;
			}
			--_finish;
		}

		void resize(const size_t& i,const T& val=T())
		{
			if (_start + i <= _endofstorage)
			{
				_finish = _start + i;
			}
			if (size() < i)
			{
				reserve(i);
				iterator it = _finish;
				while (it < _endofstorage)
				{
					*it = val;
					++ it;
				}
				_finish = _endofstorage;
			}

		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

}

